<template>

  <el-dialog v-model="centerDialogVisible" title="增加Task" width="40%" center>

    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px" style="margin-right:30px;">
      <el-form-item label="ProjectName：" prop="projectId">
        <el-select placeholder="请选择" v-model="form.taskProjectId" filterable :disabled="form.isUpdate"
                   style="width: 100%">
          <el-option
              v-for="item in allProjectArr"
              :key="item.id"
              :label="item.projectName"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="ProjectName：" prop="taskGroupId">
        <el-select placeholder="请选择" v-model="form.taskGroupId" filterable :disabled="form.isUpdate"
                   style="width: 100%">
          <el-option
              v-for="item in allGroupArr"
              :key="item.id"
              :label="item.groupName"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="name：" prop="name">
        <el-input v-model="form.name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="remark：" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="cron：" prop="cron">
        <el-input v-model="form.cron" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="isFailMail：" prop="isFailMail">
        <el-switch v-model="form.isFailMail"/>
      </el-form-item>
      <el-form-item label="status：" prop="status">
        <el-input v-model="form.status" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="statusMsg：" prop="statusMsg">
        <el-input v-model="form.statusMsg" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="serviceId：" prop="serviceId">
        <el-input v-model="form.serviceId" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="failMailRule：" prop="failMailRule">
        <el-input v-model="form.failMailRule" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="referenceType：" prop="referenceType">
        <el-input v-model="form.referenceType" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="referenceId：" prop="referenceId">
        <el-input v-model="form.referenceId" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submit">Submit</el-button>
        <el-button @click="handleClose">Cancel</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import {defineComponent} from 'vue'
import TaskGroupApi from "@/api/project/taskGroupApi"
import TaskProjectApi from "@/api/project/taskProjectApi"
import TaskJobApi from "@/api/project/taskJobApi"
import {ElMessage} from "element-plus"

export default defineComponent({
  name: 'AddJobDialog',
  props: {
    centerDialogVisible: {
      type: Boolean,
      default: false
    },
    currentRow: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      form: {
        taskProjectId: '',
        taskGroupId: '',
        groupName: '',
        isFailMail: false,
        name: '',
        remark: '',
        cron: '',
        status: '',
        statusMsg: '',
        serviceId: undefined,
        failMailRule: undefined,
        referenceType: undefined,
        referenceId: undefined,
        isUpdate: false
      },
      rules: {
        // taskGroupId: [{required: true, message: '请输入', trigger: 'blur'}],
        // referenceType: [{required: true, message: '请输入', trigger: 'blur'}],
        // referenceId: [{required: true, message: '请输入', trigger: 'blur'}]
      },
      allProjectArr: [],
      allGroupArr: []
    }
  },
  watch: {
    async currentRow(newVal) {
      if (newVal.id) {
        this.form = newVal
        this.form.isUpdate = true
        this.allProjectArr = await TaskProjectApi.simpleAll()
        this.allGroupArr = await TaskGroupApi.getSimpleGroupByProjectId(newVal.taskProjectId)
      } else {
        this.resetForm()
      }
    },
    async 'form.taskProjectId'(newVal) {
      if (newVal) {
        this.allGroupArr = await TaskGroupApi.getSimpleGroupByProjectId(newVal)
      }
    }
  },
  async mounted() {
    this.allProjectArr = await TaskProjectApi.simpleAll()
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    async submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // update
          if (this.form.isUpdate) {
            this.form.taskJobId = this.form.id
            await TaskJobApi.add(this.form)
          } else {
            // create
            await TaskJobApi.add(this.form)
          }
          ElMessage({
            message: this.form.isUpdate ? '更新成功' : "添加成功",
            type: 'success',
            duration: 1000
          })
          this.handleClose()
        }
      })
    },
    resetForm() {
      this.form = {
        taskProjectId: '',
        taskGroupId: '',
        groupName: '',
        isFailMail: false,
        name: '',
        remark: '',
        cron: '',
        status: '',
        statusMsg: '',
        serviceId: undefined,
        failMailRule: undefined,
        referenceType: undefined,
        referenceId: undefined,
        isUpdate: false
      }
    }
  }
})
</script>

<style lang="scss" scoped>

</style>